package com.lingnan.wanxiangxinxun.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingnan.wanxiangxinxun.constant.redisConstant;
import com.lingnan.wanxiangxinxun.entity.po.AuthorApplication;
import com.lingnan.wanxiangxinxun.entity.po.News;
import com.lingnan.wanxiangxinxun.entity.po.User;
import com.lingnan.wanxiangxinxun.entity.vo.service.WorkbenchData2;
import com.lingnan.wanxiangxinxun.entity.vo.service.NewUserCount;
import com.lingnan.wanxiangxinxun.service.INewsService;
import com.lingnan.wanxiangxinxun.service.IUserService;
import com.lingnan.wanxiangxinxun.service.WorkBenchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class WorkBenchServiceImpl implements WorkBenchService {
    @Autowired
    private INewsService newsService;
    @Autowired
    private IUserService userService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate_String;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Map<String, Long>> getCardBox1Data() {

        //1.查询总新闻数
        Long newsCount = newsService.count();

        //2.查询今日新增新闻数
        QueryWrapper<News> newsQueryWrapper = new QueryWrapper<>();
        newsQueryWrapper.ge("DATE(create_time)", LocalDate.now());
        Long todayNewsCount = newsService.count(newsQueryWrapper);

        //3.查询注册用户数量
        Long userCount = userService.count();

        //4.查询今日新增用户数量ws
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.ge("DATE(create_time)", LocalDate.now());
        Long todayUserCount = userService.count(userQueryWrapper);

        //5.统一返回数据
        List<Map<String, Long>> resList = new ArrayList<>();
        resList.add(Map.of("newsCount", newsCount));
        resList.add(Map.of("todayNewsCount", todayNewsCount));
        resList.add(Map.of("userCount", userCount));
        resList.add(Map.of("todayUserCount", todayUserCount));

        return resList;
    }

    //获取近7天新增用户数量
    @Override
    public List<NewUserCount> getWeekUserCount() {
        //获取近7天的日期
        List<LocalDate> dateList = new ArrayList<>();
        for (int i = 6; i >= 0; i--) { // 从小到大生成日期
            dateList.add(LocalDate.now().minusDays(i));
        }
        List<NewUserCount> resList = new ArrayList<>();
        for (LocalDate date : dateList) {
            QueryWrapper<User> qw = new QueryWrapper<>();
            qw.eq("DATE(create_time)", date);
            Long count = userService.count(qw);

            resList.add(new NewUserCount(date, count));
        }
        return resList;
    }

    //获取需要的数据
    @Override
    public WorkbenchData2 getData2() {

        WorkbenchData2 WD2 = new WorkbenchData2();
        //1.获取作者申请数
        long AuthorApplicationCount = mongoTemplate.count(new Query(), AuthorApplication.class);
        WD2.setAuthorApplyNum((int) AuthorApplicationCount);

        //2.获取待审核新闻数、头条新闻id以及标题
        QueryWrapper<News> qw = new QueryWrapper<>();
        qw.eq("status", 0);
        long count = newsService.count(qw);
        WD2.setNewsPendingApprovalNum(Math.toIntExact(count));

        //3.头条新闻id以及标题
        String headlineId = redisTemplate_String.opsForValue().get(redisConstant.HEADLINE_NEWS_KEY);
        News headline;
        if(headlineId != null){
            headline = newsService.getById(headlineId);
        }else {
            headline = null;
        }
        WD2.setHeadlineNewsId(Integer.valueOf(headlineId));
        WD2.setHeadlineNewsTitle(headline.getTitle());

        return WD2;
    }


}
